package com.todo.content.infrastructure.repository.impl;

import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.content.infrastructure.entity.condition.TopicRelationCondition;
import com.todo.content.infrastructure.entity.po.ContentTopicRelationPO;
import com.todo.content.infrastructure.mapper.ContentTopicRelationMapper;
import com.todo.content.infrastructure.repository.ContentTopicRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ContentTopicRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("contentTopicRelationRepository")
public class ContentTopicRelationRepositoryImpl implements ContentTopicRelationRepository {
    @Resource
    private ContentTopicRelationMapper contentTopicRelationMapper;


    @Override
    public Boolean insertContentTopicRelationPO(ContentTopicRelationPO contentTopicRelationPO) {
        if (contentTopicRelationPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (contentTopicRelationPO.getContentId() == null || contentTopicRelationPO.getTopicCode() == null || contentTopicRelationPO.getTopicName() == null) {
            return false;
        }
        return contentTopicRelationMapper.insertContentTopicRelationPO(contentTopicRelationPO) > 0;
    }

    @Override
    public Boolean removeContentTopicRelationPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentTopicRelationPO contentTopicRelationPO = queryContentTopicRelationPOById(id);
        if (contentTopicRelationPO == null) {
            return true;
        }
        return contentTopicRelationMapper.removeContentTopicRelationPOById(id) > 0;
    }

    @Override
    public ContentTopicRelationPO queryContentTopicRelationPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return contentTopicRelationMapper.queryContentTopicRelationPOById(id);
    }


    @Override
    public List<ContentTopicRelationPO> queryContentTopicRelationPOByContentId(Long contentId) {
        if (contentId == null || contentId < 0) {
            return Lists.newArrayList();
        }
        return contentTopicRelationMapper.queryContentTopicRelationPOByContentId(contentId);
    }

    @Override
    public Boolean removeTopicRelationByContentId(Long contentId) {
        if (contentId == null || contentId < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return contentTopicRelationMapper.removeTopicRelationPOByContentId(contentId);
    }

    @Override
    public Boolean batchInsertRelation(List<ContentTopicRelationPO> relationPOList) {
        if (CollectionUtils.isEmpty(relationPOList)) {
            return false;
        }
        for (ContentTopicRelationPO relationPO : relationPOList) {
            if (relationPO.getContentId() == null || relationPO.getTopicCode() == null || relationPO.getTopicName() == null) {
                return false;
            }
        }
        return contentTopicRelationMapper.insertBatch(relationPOList) > 0;
    }

    @Override
    public PageResult<ContentTopicRelationPO> queryTopicRelationByPage(TopicRelationCondition condition) {
        PageResult<ContentTopicRelationPO> pageResult = new PageResult<>();
        if (condition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(condition.getPageIndex());
        pageResult.setPageSize(condition.getPageSize());
        long count = queryCountByCondition(condition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, condition.getPageSize()));
        pageResult.setTotal(count);
        List<ContentTopicRelationPO> topicRelationPOList = contentTopicRelationMapper.queryContentTopicRelationByCondition(condition);
        if (CollectionUtils.isEmpty(topicRelationPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(topicRelationPOList);
        pageResult.setCurrentIndex(topicRelationPOList.get(0).getId());
        pageResult.setHasNext(topicRelationPOList.size() >= condition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(TopicRelationCondition condition) {
        if (condition == null || condition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return contentTopicRelationMapper.queryCountByCondition(condition);
    }
}
